//
//  YD_publishConversationCell.m
//  SpeedDating
//
//  Created by smile on 2017/1/15.
//  Copyright © 2017年 isu. All rights reserved.
//

#import "FriendpublishConversationCell.h"
#import "LQPhotoViewCell.h"
#import "FriendAvatarBrowser.h"

@interface FriendpublishConversationCell()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView *pickerCollectionView;
@end

@implementation FriendpublishConversationCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+(instancetype)cellWithTableView:(UITableView*)tableView {
    static NSString *ID = @"FriendpublishConversationCell";
    FriendpublishConversationCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell==nil){
        cell = [[FriendpublishConversationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID ];
        cell.backgroundColor =[UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
        CGRect rec = CGRectMake(0, 0,KScreenWidth, (KScreenWidth-80)*0.25+20);
        self.pickerCollectionView = [[UICollectionView alloc]initWithFrame:rec collectionViewLayout:layout];
        self.pickerCollectionView.delegate=self;
        self.pickerCollectionView.dataSource=self;
        self.pickerCollectionView.backgroundColor = [UIColor whiteColor];
        _pickerCollectionView.scrollEnabled = NO;
        [self.contentView addSubview:self.pickerCollectionView];
    }
    return self;
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrPhotos.count+1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UINib *nib = [UINib nibWithNibName:@"LQPhotoViewCell" bundle: [NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"LQPhotoViewCell"];
    // Set up the reuse identifier
    LQPhotoViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"LQPhotoViewCell" forIndexPath:indexPath];
    cell.closeButton.tag = indexPath.item;
    [cell.closeButton addTarget:self action:@selector(deletePhoto:) forControlEvents:UIControlEventTouchUpInside];
    if (indexPath.row == self.arrPhotos.count) {
        [cell.profilePhoto setImage:[UIImage imageNamed: @"Friend_news_add"]];
        cell.closeButton.hidden = YES;
    }
    else{
        [cell.profilePhoto setImage:self.arrPhotos[indexPath.item]];
        cell.closeButton.hidden = NO;
    }
    cell.profilePhoto.userInteractionEnabled = YES;
    return cell;
}

#pragma mark <UICollectionViewDelegate>
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(([UIScreen mainScreen].bounds.size.width-64) /4 ,([UIScreen mainScreen].bounds.size.width-64) /4);
}

//定义每个UICollectionView 的 margin
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item==self.arrPhotos.count){
        
         [self.delegate  publishConversationCellStartGetPhotos];
    }
    else{
        LQPhotoViewCell *cell =(LQPhotoViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
        [FriendAvatarBrowser showImage:cell.profilePhoto];
        
    }
}

-(void)setArrPhotos:(NSArray *)arrPhotos{
    _arrPhotos = arrPhotos;
    NSUInteger num= 0;
    if(self.arrPhotos.count<=1){
        num =1;
    }
    else{
        num = self.arrPhotos.count;
    }
    CGRect rec = CGRectMake(0, 0,KScreenWidth, ((num)/4+1)*(KScreenWidth-80)*0.25+20*((num)/4+1));
    self.pickerCollectionView.frame = rec;
    [self.pickerCollectionView reloadData];
}
-(CGFloat)getHeight{
    [self.contentView layoutIfNeeded];
    CGFloat H = CGRectGetMaxY(self.pickerCollectionView.frame);
    return  H+10;
}

-(void)deletePhoto:(UIButton*)sender{
   // HQLog(@"sender.tag = %ld",(long)sender.tag);
    [self.delegate publishConversationCellStartDelegatePhoto:sender.tag];
}
@end
